package com.deer.wms.inventory.web;

import cn.hutool.core.date.DateUtil;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.constant.InventoryConstant;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.model.Inventory.InventoryTransact;
import com.deer.wms.inventory.model.InventoryAdjust;
import com.deer.wms.inventory.model.InventoryAdjustDetail;
import com.deer.wms.inventory.model.InventoryAdjustDetailCriteria;
import com.deer.wms.inventory.model.InventoryAdjustDetailDto;
import com.deer.wms.inventory.service.InventoryAdjustDetailService;
import com.deer.wms.inventory.service.InventoryAdjustService;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.inventory.service.InventoryTransactService;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.review.manage.model.BillRecord;
import com.deer.wms.review.manage.model.BillRecordTypeConstants;
import com.deer.wms.review.manage.service.BillRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by ll on 2020/03/12.
*/
@RestController
@RequestMapping("/inventory/adjust/details")
public class InventoryAdjustDetailController {

    @Autowired
    private InventoryAdjustDetailService inventoryAdjustDetailService;
    @Autowired
    private InventoryAdjustService inventoryAdjustService;
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private InventoryTransactService inventoryTransactService;
    @Autowired
    private BillRecordService billRecordService;

    @PostMapping
    public Result add(@RequestBody InventoryAdjustDetail inventoryAdjustDetail) {
        inventoryAdjustDetailService.save(inventoryAdjustDetail);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        inventoryAdjustDetailService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    public Result update(@RequestBody InventoryAdjustDetail inventoryAdjustDetail) {
        inventoryAdjustDetailService.updateById(inventoryAdjustDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        InventoryAdjustDetail inventoryAdjustDetail = inventoryAdjustDetailService.getById(id);
        return ResultGenerator.genSuccessResult(inventoryAdjustDetail);
    }

    @GetMapping
    public Result list(InventoryAdjustDetailCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<InventoryAdjustDetail> list = inventoryAdjustDetailService.list();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findList")
    public Result findList(InventoryAdjustDetailCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<InventoryAdjustDetailDto> list = inventoryAdjustDetailService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "调整确认", notes = "调整确认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @PostMapping("/adjustAffirm")
    @Transactional
    public Result adjustAffirm(@RequestBody InventoryAdjustDetailCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        List<InventoryAdjustDetailDto> inventoryAdjustDetailDtos = inventoryAdjustDetailService.findList(criteria);
        for (InventoryAdjustDetailDto inventoryAdjustDetailDto : inventoryAdjustDetailDtos) {
            if (inventoryAdjustDetailDto.getState().equals(1)) {
                InventoryAdjust adjust = inventoryAdjustService.getOneByField("bill_no", inventoryAdjustDetailDto.getBillNo());
                InventoryTransact inventoryTransact;
                if (adjust.getType() == 2) {
                    //手工调账
                    inventoryTransact = new InventoryTransact(inventoryAdjustDetailDto);
                    inventoryTransact.setCreateTime(DateUtil.now());
                    inventoryTransact.setCreateUserId(currentUser.getUserId());
                    inventoryTransact.setCreateUserName(currentUser.getUserName());
                } else {
                    //盘点触发
                    inventoryTransact = new InventoryTransact();
                    Integer inventoryId = inventoryAdjustDetailDto.getInventoryId();
                    Inventory inventory = inventoryService.getById(inventoryId);
                    inventoryTransact.setFromInventoryId(inventoryId);
                    //仓库
                    inventoryTransact.setWareId(inventory.getWareId());
                    inventoryTransact.setWareName(inventory.getWareName());
                    //货主
                    inventoryTransact.setFromOrganizationId(inventory.getOrganizationId());
                    inventoryTransact.setFromOrganizationName(inventory.getOrganizationName());
                    inventoryTransact.setToOrganizationId(inventory.getOrganizationId());
                    inventoryTransact.setToOrganizationName(inventory.getOrganizationName());
                    //库位
                    inventoryTransact.setFromCellCode(inventory.getCellCode());
                    inventoryTransact.setToCellCode(inventoryAdjustDetailDto.getCellCode());
                    //容器
                    inventoryTransact.setFromBoxCode(inventory.getBoxCode());
                    inventoryTransact.setToBoxCode(inventoryAdjustDetailDto.getBoxCode());
                    //物料
                    inventoryTransact.setItemCode(inventoryAdjustDetailDto.getItemCode());
                    inventoryTransact.setItemName(inventoryAdjustDetailDto.getItemName());
                    //批次
                    inventoryTransact.setToBatchId(inventoryAdjustDetailDto.getBatchId());
                    inventoryTransact.setToBatchName(inventoryAdjustDetailDto.getBatchName());
                    //数量
                    inventoryTransact.setToQuantity(inventory.getQuantity() + inventoryAdjustDetailDto.getDifferQuantity());
                    //创建人 时间
                    inventoryTransact.setCreateUserId(currentUser.getUserId());
                    inventoryTransact.setCreateUserName(currentUser.getUserName());
                    inventoryTransact.setCreateTime(DateUtil.now());
                    //事务类型
                    inventoryTransact.setTransactType(InventoryConstant.INVENTORY_ADJUST);
                    //来源单据号
                    inventoryTransact.setBillNo(inventoryAdjustDetailDto.getBillNo());
                }
                inventoryTransactService.save(inventoryTransact);
                inventoryTransactService.run(inventoryTransact);
                inventoryAdjustDetailDto.setState(2);
                inventoryAdjustDetailDto.setAdjustTime(DateUtils.getNowDateTimeString());
                inventoryAdjustDetailDto.setAdjustUserId(currentUser.getUserId());
                inventoryAdjustDetailDto.setAdjustUserName(currentUser.getUserName());
                inventoryAdjustDetailService.updateById(inventoryAdjustDetailDto);
            }
        }
        return ResultGenerator.genSuccessResult();
    }

}
